home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / WinTool.bas < prev    next >
BASIC Source File  |  1997-06-14  |  11KB  |  341 lines

  1. Attribute VB_Name = "MWinTool"
  2. Option Explicit
  3.  
  4. Public Enum EErrorWinTool
  5.     eeBaseWinTool = 13640   ' WinTool
  6. End Enum
  7.  
  8. #If fComponent Then
  9. Sub SetRedraw(ctl As Object, f As Boolean)
  10. #Else
  11. Sub SetRedraw(ctl As Control, f As Boolean)
  12. #End If
  13.     Call SendMessageVal(ctl.hWnd, WM_SETREDRAW, -CLng(f), 0&)
  14. End Sub
  15.  
  16. #If fComponent Then
  17. Function LookupItemData(ctl As Object, data As Long) As Integer
  18. #Else
  19. Function LookupItemData(ctl As Control, data As Long) As Integer
  20. #End If
  21.     Dim i As Integer
  22.     LookupItemData = -1
  23.     For i = 0 To ctl.ListCount - 1
  24.         If data = ctl.ItemData(i) Then
  25.             LookupItemData = i
  26.             Exit Function
  27.         End If
  28.     Next
  29. End Function
  30.  
  31. #If fComponent Then
  32. Function LookupItem(ctl As Object, sItem As String) As Long
  33. #Else
  34. Function LookupItem(ctl As Control, sItem As String) As Long
  35. #End If
  36.     If TypeName(ctl) = "ComboBox" Then
  37.         LookupItem = SendMessageStr(ctl.hWnd, CB_FINDSTRING, -1&, sItem)
  38.     Else
  39.         LookupItem = SendMessageStr(ctl.hWnd, LB_FINDSTRING, -1&, sItem)
  40.     End If
  41. End Function
  42.  
  43. Function ClassNameFromWnd(ByVal hWnd As Long) As String
  44.     Dim sName As String, cName As Integer
  45.     BugAssert hWnd <> hNull
  46.     sName = String$(80, 0)
  47.     cName = GetClassName(hWnd, sName, 80)
  48.     ClassNameFromWnd = Left$(sName, cName)
  49. End Function
  50.  
  51. Function InstFromWnd(ByVal hWnd As Long) As Long
  52.     BugAssert hWnd <> hNull
  53.     InstFromWnd = GetWindowLong(hWnd, GWL_HINSTANCE)
  54. End Function
  55.  
  56. Function ProcIDFromWnd(ByVal hWnd As Long) As Long
  57.     Dim idProc As Long
  58.     Call GetWindowThreadProcessId(hWnd, idProc)
  59.     ProcIDFromWnd = idProc
  60. End Function
  61.  
  62. Function ProcFromWnd(ByVal hWnd As Long) As Long
  63.     BugAssert hWnd <> hNull
  64.     ProcFromWnd = MModTool.ProcFromProcID(ProcIDFromWnd(hWnd))
  65. End Function
  66.  
  67. Function ThreadIDFromWnd(ByVal hWnd As Long) As Long
  68.     Dim idProc As Long
  69.     BugAssert hWnd <> hNull
  70.     ThreadIDFromWnd = GetWindowThreadProcessId(hWnd, idProc)
  71. End Function
  72.  
  73. Function GetWndOwner(ByVal hWnd As Long) As String
  74.     Dim hwndOwner As Long
  75.     BugAssert hWnd <> hNull
  76.     hwndOwner = GetWindow(hWnd, GW_OWNER)
  77.     If hwndOwner <> hNull Then
  78.         GetWndOwner = WindowTextLineFromWnd(hwndOwner)
  79.     Else
  80.         GetWndOwner = sEmpty
  81.     End If
  82. End Function
  83.  
  84. Function IsWindowLocal(ByVal hWnd As Long) As Boolean
  85.     Dim idWnd As Long
  86.     Call GetWindowThreadProcessId(hWnd, idWnd)
  87.     IsWindowLocal = (idWnd = GetCurrentProcessId())
  88. End Function
  89.  
  90. Function IsVisibleTopWnd(hWnd As Long, _
  91.                 Optional IgnoreEmpty As Boolean = False, _
  92.                 Optional IgnoreVisible As Boolean = False, _
  93.                 Optional IgnoreOwned As Boolean = False) _
  94.                 As Boolean
  95.     If IgnoreEmpty Or WindowTextFromWnd(hWnd) <> sEmpty Then
  96.         If IgnoreVisible Or IsWindowVisible(hWnd) Then
  97.             If IgnoreOwned Or GetWindow(hWnd, GW_OWNER) = hNull Then
  98.                 IsVisibleTopWnd = True
  99.             End If
  100.         End If
  101.     End If
  102. End Function
  103.  
  104. Function VBFindWindow(Optional Class As String, _
  105.                       Optional Title As String) As Long
  106.     VBFindWindow = FindWindow(Class, Title)
  107. End Function
  108.  
  109. Function WindowTextFromWnd(ByVal hWnd As Long) As String
  110.     Dim c As Integer, s As String
  111.     c = GetWindowTextLength(hWnd)
  112.     If c <= 0 Then Exit Function
  113.     s = String$(c, 0)
  114.     c = GetWindowText(hWnd, s, c + 1)
  115.     WindowTextFromWnd = s
  116. End Function
  117.  
  118. Function WindowTextLineFromWnd(ByVal hWnd As Long) As String
  119.     Dim sTitle As String, cTitle As Integer
  120.     sTitle = WindowTextFromWnd(hWnd)
  121.     ' Chop off end of multiline captions
  122.     cTitle = InStr(sTitle, sCr)
  123.     WindowTextLineFromWnd = IIf(cTitle, Left$(sTitle, cTitle), sTitle)
  124. End Function
  125.  
  126. Function VBFindTopWindow(sClass As String, sTitle As String) As Long
  127.                        
  128.     ' Assume fail for easy exit
  129.     VBFindTopWindow = hNull
  130.     If sClass = sEmpty And sTitle = sEmpty Then Exit Function
  131.  
  132.     ' Get first sibling to start iterating top level windows
  133.     Dim fClass As Boolean, fTitle As Boolean
  134.     Dim hWnd As Long
  135.     hWnd = GetWindow(GetDesktopWindow(), GW_CHILD)
  136.     Do While hWnd <> hNull
  137.         
  138.         ' Check class
  139.         fClass = True
  140.         If sClass <> sEmpty Then
  141.             fClass = (StrComp(sClass, ClassNameFromWnd(hWnd)) = 0)
  142.         End If
  143.         
  144.         ' Check title
  145.         fTitle = True
  146.         If sTitle <> sEmpty Then
  147.             fTitle = (WindowTextFromWnd(hWnd) Like sTitle)
  148.         End If
  149.  
  150.         ' Check success
  151.         If fClass And fTitle Then
  152.             VBFindTopWindow = hWnd
  153.             Exit Function
  154.         End If
  155.  
  156.         ' Get next sibling
  157.         hWnd = GetWindow(hWnd, GW_HWNDNEXT)
  158.     Loop
  159.  
  160. End Function
  161.  
  162. Sub ChangeStyleBit(hWnd As Long, f As Boolean, afNew As Long)
  163.     Dim af As Long, hParent As Long
  164.     af = GetWindowLong(hWnd, GWL_STYLE)
  165.     If f Then
  166.         af = af Or afNew
  167.     Else
  168.         af = af And (Not afNew)
  169.     End If
  170.     Call SetWindowLong(hWnd, GWL_STYLE, af)
  171.     ' Reset the parent so that change will "take"
  172.     hParent = GetParent(hWnd)
  173.     SetParent hWnd, hParent
  174.     ' Redraw for added insurance
  175.     Call SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, _
  176.                       SWP_NOZORDER Or SWP_NOSIZE Or _
  177.                       SWP_NOMOVE Or SWP_DRAWFRAME)
  178. End Sub
  179.  
  180. Function GetStyleBits(hWnd As Long) As Long
  181.     GetStyleBits = GetWindowLong(hWnd, GWL_STYLE)
  182. End Function
  183.  
  184. Sub ChangeExtStyleBit(hWnd As Long, f As Boolean, afNew As Long)
  185.     Dim af As Long, hParent As Long
  186.     af = GetWindowLong(hWnd, GWL_EXSTYLE)
  187.     If f Then
  188.         af = af Or afNew
  189.     Else
  190.         af = af And (Not afNew)
  191.     End If
  192.     Call SetWindowLong(hWnd, GWL_EXSTYLE, af)
  193.     ' Reset the parent so that change will "take"
  194.     hParent = GetParent(hWnd)
  195.     SetParent hWnd, hParent
  196.     ' Redraw for added insurance
  197.     Call SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, _
  198.                       SWP_NOZORDER Or SWP_NOSIZE Or _
  199.                       SWP_NOMOVE Or SWP_DRAWFRAME)
  200. End Sub
  201.  
  202. Function GetExtStyleBits(hWnd As Long) As Long
  203.     GetExtStyleBits = GetWindowLong(hWnd, GWL_EXSTYLE)
  204. End Function
  205.  
  206. ' Something that uses ChangeStyleBit
  207.  
  208. Sub SetClipControls(hWnd As Long, f As Boolean)
  209.     ' You want to do this:
  210.     'Me.ClipControls = f
  211.     ' But Visual Basic won't let you; do this instead:
  212.     ChangeStyleBit hWnd, f, WS_CLIPCHILDREN
  213. End Sub
  214.     
  215. Sub ClientToScreenXY(ByVal hWnd As Long, x As Long, y As Long)
  216.     Dim pt As POINTL
  217.     pt.x = x \ Screen.TwipsPerPixelX
  218.     pt.y = y \ Screen.TwipsPerPixelY
  219.     ClientToScreen hWnd, pt
  220.     x = pt.x
  221.     y = pt.y
  222. End Sub
  223.         
  224. Function GetWndStyle(hWnd) As String
  225.     Dim af As Long, s As String
  226.     BugAssert hWnd <> hNull
  227.  
  228.     ' Get normal style
  229.     af = GetWindowLong(hWnd, GWL_STYLE)
  230.     If af And WS_BORDER Then s = s & "Border "
  231.     If af And WS_CAPTION Then s = s & "Caption "
  232.     If af And WS_CHILD Then s = s & "Child "
  233.     If af And WS_CLIPCHILDREN Then s = s & "ClipChildren "
  234.     If af And WS_CLIPSIBLINGS Then s = s & "ClipSiblings "
  235.     If af And WS_DLGFRAME Then s = s & "DlgFrame "
  236.     If af And WS_GROUP Then s = s & "Group "
  237.     If af And WS_HSCROLL Then s = s & "HScroll "
  238.     If af And WS_MAXIMIZEBOX Then s = s & "MaximizeBox "
  239.     If af And WS_MINIMIZEBOX Then s = s & "MinimizeBox "
  240.     If af And WS_POPUP Then s = s & "Popup "
  241.     If af And WS_SYSMENU Then s = s & "SysMenu "
  242.     If af And WS_TABSTOP Then s = s & "TabStop "
  243.     If af And WS_THICKFRAME Then s = s & "ThickFrame "
  244.     If af And WS_VSCROLL Then s = s & "VScroll "
  245.  
  246.     ' Get extended style
  247.     af = GetWindowLong(hWnd, GWL_EXSTYLE)
  248.     If af And WS_EX_DLGMODALFRAME Then s = s & "DlgModalFrame "
  249.     If af And WS_EX_NOPARENTNOTIFY Then s = s & "NoParentNotify "
  250.     If af And WS_EX_TOPMOST Then s = s & "Topmost "
  251.     If af And WS_EX_ACCEPTFILES Then s = s & "AcceptFiles "
  252.     If af And WS_EX_TRANSPARENT Then s = s & "Transparent "
  253.  
  254.     GetWndStyle = s
  255.  
  256. End Function
  257.  
  258. Public Function GetWndInfo(ByVal hWnd As Long, Optional TabStop As Integer = 0) As String
  259.     Dim sStart As String, s As String, sTemp As String
  260.     BugAssert hWnd <> hNull
  261.     
  262.     ' Nested starting position
  263.     sStart = Space$(TabStop * 4)
  264.     ' Window information
  265.     sTemp = WindowTextLineFromWnd(hWnd)
  266.     'sTemp = WindowTextFromWnd(hWnd)
  267.     If sTemp = sEmpty Then sTemp = "[none]"
  268.     s = sStart & "Title: " & sTemp & sCrLf
  269.     s = s & sStart & "Class: " & ClassNameFromWnd(hWnd) & sCrLf
  270.     s = s & sStart & "Style: " & GetWndStyle(hWnd) & sCrLf
  271.     sTemp = GetWndOwner(hWnd)
  272.     If sTemp <> sEmpty Then
  273.         s = s & sStart & "Owner: " & sTemp & sCrLf
  274.     End If
  275.  
  276.     GetWndInfo = s
  277.  
  278. End Function
  279.  
  280. Public Function GetWndView(hWnd) As String
  281.     Dim s As String
  282.     BugAssert hWnd <> hNull
  283.     s = IIf(IsWindowVisible(hWnd), "Visible ", "Invisible ")
  284.     s = s & IIf(IsWindowEnabled(hWnd), "Enabled", "Disabled ")
  285.     s = s & IIf(IsZoomed(hWnd), "Zoomed ", sEmpty)
  286.     s = s & IIf(IsIconic(hWnd), "Iconic ", sEmpty)
  287.     GetWndView = s
  288. End Function
  289.  
  290. Function GetTextExtentWnd(ByVal hWnd As Long, s As String, _
  291.                      Optional dy As Single) As Single
  292.                      
  293.     Dim hDC As Long, sz As SIZEL, f As Long
  294.     hDC = GetDC(hWnd)
  295.     f = GetTextExtentPoint32(hDC, s, Len(s), sz)
  296.     If f Then
  297.         ' Most common x value in return
  298.         GetTextExtentWnd = sz.cx
  299.         ' Optional y value through reference variable
  300.         dy = sz.cy
  301.     End If
  302.     Call ReleaseDC(hWnd, hDC)
  303.                      
  304. End Function
  305.  
  306. Property Get WindowsDir() As String
  307.     Dim s As String, c As Long
  308.     s = String$(cMaxPath, 0)
  309.     c = GetWindowsDirectory(s, cMaxPath)
  310.     WindowsDir = Left(s, c)
  311. End Property
  312.  
  313. Property Get SystemDir() As String
  314.     Dim s As String, c As Long
  315.     s = String$(cMaxPath, 0)
  316.     c = GetSystemDirectory(s, cMaxPath)
  317.     SystemDir = Left(s, c)
  318. End Property
  319. '
  320.  
  321. #If fComponent = 0 Then
  322. Private Sub ErrRaise(e As Long)
  323.     Dim sText As String, sSource As String
  324.     If e > 1000 Then
  325.         sSource = App.ExeName & ".WinTool"
  326.         Select Case e
  327.         Case eeBaseWinTool
  328.             BugAssert True
  329.        ' Case ee...
  330.        '     Add additional errors
  331.         End Select
  332.         Err.Raise COMError(e), sSource, sText
  333.     Else
  334.         ' Raise standard Visual Basic error
  335.         sSource = App.ExeName & ".VBError"
  336.         Err.Raise e, sSource
  337.     End If
  338. End Sub
  339. #End If
  340.  
  341.